#!/bin/bash

# 快捷部署脚本
echo "=== 币安K线分析系统管理脚本 ==="
echo "注意: 本项目不在本地启动，只在服务器操作"
echo "服务器: 182.160.6.140，应用路径: /var/www/binance-analysis"

# 如果没有参数，则默认为部署
if [ $# -lt 1 ]; then
  ACTION="deploy"
else
  ACTION=$1
fi

case $ACTION in
  deploy)
    # 执行部署
    echo "开始部署到服务器..."
    chmod +x ./scripts/update.sh
    ./scripts/update.sh
    
    echo "部署完成!"
    echo "记得补充docs目录下的文档并提交git"
    ;;

  # 全部服务管理命令
  start_all)
    echo "启动所有服务..."
    chmod +x ./scripts/start_app.sh ./scripts/start_cache.sh ./scripts/start_monitor.sh
    ./scripts/start_app.sh
    ./scripts/start_cache.sh
    ./scripts/start_monitor.sh
    ;;
  
  stop_all)
    echo "停止所有服务..."
    chmod +x ./scripts/service_manager.sh
    ./scripts/service_manager.sh stop_all
    ;;
  
  restart_all)
    echo "重启所有服务..."
    chmod +x ./scripts/service_manager.sh ./scripts/start_app.sh ./scripts/start_cache.sh ./scripts/start_monitor.sh
    ./scripts/service_manager.sh stop_all
    sleep 3
    ./scripts/start_app.sh
    ./scripts/start_cache.sh
    ./scripts/start_monitor.sh
    ;;
  
  # 主应用管理命令
  start_app)
    echo "启动主应用程序..."
    chmod +x ./scripts/start_app.sh
    ./scripts/start_app.sh
    ;;
  
  stop_app)
    echo "停止主应用程序..."
    chmod +x ./scripts/service_manager.sh
    ./scripts/service_manager.sh stop_app
    ;;
  
  restart_app)
    echo "重启主应用程序..."
    chmod +x ./scripts/service_manager.sh ./scripts/start_app.sh
    ./scripts/service_manager.sh stop_app
    sleep 2
    ./scripts/start_app.sh
    ;;
  
  # 缓存服务管理命令
  start_cache)
    echo "启动缓存服务..."
    chmod +x ./scripts/start_cache.sh
    ./scripts/start_cache.sh
    ;;
  
  stop_cache)
    echo "停止缓存服务..."
    chmod +x ./scripts/service_manager.sh
    ./scripts/service_manager.sh stop_cache
    ;;
  
  restart_cache)
    echo "重启缓存服务..."
    chmod +x ./scripts/service_manager.sh ./scripts/start_cache.sh
    ./scripts/service_manager.sh stop_cache
    sleep 2
    ./scripts/start_cache.sh
    ;;
  
  # 监控服务管理命令
  start_monitor)
    echo "启动监控服务..."
    chmod +x ./scripts/start_monitor.sh
    ./scripts/start_monitor.sh
    ;;
  
  stop_monitor)
    echo "停止监控服务..."
    chmod +x ./scripts/service_manager.sh
    ./scripts/service_manager.sh stop_monitor
    ;;
  
  restart_monitor)
    echo "重启监控服务..."
    chmod +x ./scripts/service_manager.sh ./scripts/start_monitor.sh
    ./scripts/service_manager.sh stop_monitor
    sleep 2
    ./scripts/start_monitor.sh
    ;;
  
  status)
    echo "查看服务状态..."
    chmod +x ./scripts/service_manager.sh
    ./scripts/service_manager.sh status
    ;;
  
  manage)
    echo "进入服务管理..."
    echo "可用命令列表:"
    chmod +x ./scripts/service_manager.sh
    ./scripts/service_manager.sh
    ;;
  
  *)
    echo "未知操作: $ACTION"
    echo "用法: $0 [命令]"
    echo "可用命令:"
    echo "  deploy         - 部署应用"
    echo "  -- 全部服务管理 --"
    echo "  start_all      - 启动所有服务"
    echo "  stop_all       - 停止所有服务"
    echo "  restart_all    - 重启所有服务"
    echo "  -- 主应用管理 --"
    echo "  start_app      - 启动主应用程序"
    echo "  stop_app       - 停止主应用程序"
    echo "  restart_app    - 重启主应用程序"
    echo "  -- 缓存服务管理 --"
    echo "  start_cache    - 启动缓存服务"
    echo "  stop_cache     - 停止缓存服务"
    echo "  restart_cache  - 重启缓存服务"
    echo "  -- 监控服务管理 --"
    echo "  start_monitor  - 启动监控服务"
    echo "  stop_monitor   - 停止监控服务"
    echo "  restart_monitor- 重启监控服务"
    echo "  -- 其他命令 --"
    echo "  status         - 查看所有服务状态"
    echo "  manage         - 显示服务管理选项"
    exit 1
    ;;
esac 